package me.zhengjie.sqlservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.mapper.WcsInboundMissionMapper;
import me.zhengjie.pojo.entity.WcsInboundMission;
import me.zhengjie.sqlservice.WcsInboundMissionService;
import me.zhengjie.state.TaskStatus;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class WcsInboundMissionServiceImpl extends ServiceImpl<WcsInboundMissionMapper,WcsInboundMission> implements WcsInboundMissionService {

    private final WcsInboundMissionMapper wcsInboundMissionMapper;

    @Override
    public boolean insertInbound(WcsInboundMission wcsInboundMission) {
        return wcsInboundMissionMapper.insert(wcsInboundMission) > 0;
    }

    @Override
    public List<WcsInboundMission> selectContainerCode(String containerCode) {
        LambdaQueryWrapper<WcsInboundMission> wcsInboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsInboundMissionLambdaQueryWrapper.eq(WcsInboundMission::getContainerCode, containerCode)
                .eq(WcsInboundMission::getMissionStatus,0)
                .orderByDesc(WcsInboundMission::getUpdateTime);
        return wcsInboundMissionMapper.selectList(wcsInboundMissionLambdaQueryWrapper);
    }

    @Override
    public boolean updateMissionStatus(String inboundCode, Integer missionStatus) {
        LambdaUpdateWrapper<WcsInboundMission> wcsInboundMissionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsInboundMissionLambdaUpdateWrapper.eq(WcsInboundMission::getInboundCode, inboundCode)
                .set(WcsInboundMission::getMissionStatus, missionStatus)
                .set(WcsInboundMission::getUpdateTime, LocalDateTime.now());
        return wcsInboundMissionMapper.update(null,wcsInboundMissionLambdaUpdateWrapper) > 0;
    }

    @Override
    public WcsInboundMission selectInboundCode(String robotTaskCode) {
        LambdaQueryWrapper<WcsInboundMission> wcsInboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsInboundMissionLambdaQueryWrapper.eq(WcsInboundMission::getInboundCode,robotTaskCode);
        return wcsInboundMissionMapper.selectOne(wcsInboundMissionLambdaQueryWrapper);
    }

    @Override
    public boolean updateMissionStatusAndAgvId(Integer id, Integer missionStatus, String agvId) {
        LambdaUpdateWrapper<WcsInboundMission> wcsInboundMissionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsInboundMissionLambdaUpdateWrapper.set(WcsInboundMission::getMissionStatus, missionStatus)
                .set(WcsInboundMission::getAgvId, agvId)
                .set(WcsInboundMission::getUpdateTime,LocalDateTime.now())
                .eq(WcsInboundMission::getId, id);
        return wcsInboundMissionMapper.update(null,wcsInboundMissionLambdaUpdateWrapper) > 0;
    }

    @Override
    public boolean deleteInboundId(Integer id) {
        return wcsInboundMissionMapper.deleteById(id) > 0;
    }

    @Override
    public List<WcsInboundMission> selectStatusAndDestinationAndContainerTypePage(TaskStatus missionStatus, String destination, Integer containerType, Integer size) {
        Page<WcsInboundMission> wcsInboundMissionPage = new Page<>(1, size);
        LambdaQueryWrapper<WcsInboundMission> wcsInboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsInboundMissionLambdaQueryWrapper.eq(WcsInboundMission::getMissionStatus,missionStatus)
                .eq(WcsInboundMission::getDestination,destination)
                .eq(WcsInboundMission::getContainerType,containerType);
        return wcsInboundMissionMapper.selectPage(wcsInboundMissionPage,wcsInboundMissionLambdaQueryWrapper).getRecords();
    }

    @Override
    public List<WcsInboundMission> selectContainerTypeAndInboundType(Integer containerType, Integer inboundType) {
        LambdaQueryWrapper<WcsInboundMission> wcsInboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsInboundMissionLambdaQueryWrapper.eq(WcsInboundMission::getContainerType,containerType)
                .eq(WcsInboundMission::getInboundType,inboundType);
        return wcsInboundMissionMapper.selectList(wcsInboundMissionLambdaQueryWrapper);
    }

    @Override
    public List<WcsInboundMission> selectMissionStatus(Integer status) {
        LambdaQueryWrapper<WcsInboundMission> wcsInboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsInboundMissionLambdaQueryWrapper.eq(WcsInboundMission::getMissionStatus,status);
        return wcsInboundMissionMapper.selectList(wcsInboundMissionLambdaQueryWrapper);
    }

    @Override
    public List<WcsInboundMission> selectContainerCodeUpdateDesc(String containerCode) {
        LambdaQueryWrapper<WcsInboundMission> wcsInboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsInboundMissionLambdaQueryWrapper.eq(WcsInboundMission::getContainerCode,containerCode)
                .orderByDesc(WcsInboundMission::getUpdateTime);
        return wcsInboundMissionMapper.selectList(wcsInboundMissionLambdaQueryWrapper);
    }

    @Override
    public boolean updateDestination(String inboundCode, String destination) {
        LambdaUpdateWrapper<WcsInboundMission> wcsInboundMissionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsInboundMissionLambdaUpdateWrapper.set(WcsInboundMission::getDestination,destination)
                .eq(WcsInboundMission::getInboundCode,inboundCode);
        return wcsInboundMissionMapper.update(null,wcsInboundMissionLambdaUpdateWrapper) >0;
    }

    @Override
    public List<WcsInboundMission> selectLtStatus(Integer status) {
        LambdaQueryWrapper<WcsInboundMission> wcsInboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsInboundMissionLambdaQueryWrapper.lt(WcsInboundMission::getMissionStatus,status);
        return wcsInboundMissionMapper.selectList(wcsInboundMissionLambdaQueryWrapper);
    }

    @Override
    public WcsInboundMission selectStorageAndContainerCode(String source, String containerCode) {
        LambdaQueryWrapper<WcsInboundMission> wcsInboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsInboundMissionLambdaQueryWrapper.eq(WcsInboundMission::getSource,source)
                .eq(WcsInboundMission::getContainerCode,containerCode)
                .notIn(WcsInboundMission::getMissionStatus,4,5);
        return wcsInboundMissionMapper.selectOne(wcsInboundMissionLambdaQueryWrapper);
    }

    @Override
    public List<WcsInboundMission> queryEmptyMaterialBox(List<String> sourceList, Integer containerType, Integer page, Integer size) {
        log.info("查询分页数据：{},{}",page,size);
        //创建分页对象
        Page<WcsInboundMission> wcsInboundMissionPage = new Page<>(page, size);

        // 创建查询条件构造器
        LambdaQueryWrapper<WcsInboundMission> wcsInboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsInboundMissionLambdaQueryWrapper.in(WcsInboundMission::getSource, sourceList)
                .eq(WcsInboundMission::getContainerType, containerType)
                .orderByDesc(WcsInboundMission::getCreateTime);

        // 执行分页查询
        List<WcsInboundMission> wcsInboundMissionList = wcsInboundMissionMapper.selectPage(wcsInboundMissionPage,wcsInboundMissionLambdaQueryWrapper).getRecords();

        // 返回分页结果
        return wcsInboundMissionList;
    }

    @Override
    public Integer queryEmptyMaterialBoxCount(List<String> sourceList, Integer containerType) {
        LambdaQueryWrapper<WcsInboundMission> wcsInboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsInboundMissionLambdaQueryWrapper.in(WcsInboundMission::getSource,sourceList)
                .eq(WcsInboundMission::getContainerType,containerType);
        return Math.toIntExact(wcsInboundMissionMapper.selectCount(wcsInboundMissionLambdaQueryWrapper));
    }

    @Override
    public List<WcsInboundMission> selectStorageAndContainerCodeS(String source, String containerCode) {
            LambdaQueryWrapper<WcsInboundMission> wcsInboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
            wcsInboundMissionLambdaQueryWrapper.eq(WcsInboundMission::getSource,source)
                    .eq(WcsInboundMission::getContainerCode,containerCode)
                    .notIn(WcsInboundMission::getMissionStatus,4,5);
            return wcsInboundMissionMapper.selectList(wcsInboundMissionLambdaQueryWrapper);
    }
}
